package com.example.android_is2;

import java.util.ArrayList;

import com.example.android_is2.pathDraw.GoogleParser;
import com.example.android_is2.pathDraw.Route;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

public class DialogBackground extends AsyncTask<String,String,String> {
	//Route route = directions_wayspoint(listView, modeLocation);
	private Route route;
	private ProgressDialog pd;
	private ArrayList<String> listView;
	private String modelocation;
	
	
	public DialogBackground(MapRouteActivitySelection mapRouteActivitySelection) {
		// TODO Auto-generated constructor stub
		pd=ProgressDialog.show(mapRouteActivitySelection,"Attendere:","Caricamento in corso...",true,false);
	}

	public void settRoute(Route route){
		this.route = route;
	}
	
	public Route getRoute(){
		return route;
	}
	@Override
	protected String doInBackground(String... arg0) {
		// TODO Auto-generated method stub
		this.route=directions_wayspoint(listView,modelocation);
		publishProgress(null);
		return null;
	}

	@Override
	protected void onProgressUpdate(String... values) {
		// TODO Auto-generated method stub
		super.onProgressUpdate(values);
		pd.dismiss();
	}

	private Route directions_wayspoint(@SuppressWarnings("hiding")
			  final ArrayList<String> listView, String modeLocation) {
			    GoogleParser parser;
			    String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
			    final StringBuffer sBuf = new StringBuffer(jsonURL);
			    sBuf.append("origin=");
			    sBuf.append(listView.get(0).toLowerCase());

			    sBuf.append("&destination=");

			    sBuf.append(listView.get(listView.size() - 1).toLowerCase());
			    if (listView.size() > 2) {
			      sBuf.append("&waypoints=");
			      for (int i = 1; i < listView.size() - 1; i++) {
			        if (i == 1) {
			          sBuf.append(listView.get(i).toLowerCase());
			        } else {
			          sBuf.append("|" + listView.get(i).toLowerCase());
			        }
			      }
			    }
			    sBuf.append("&sensor=true");
			    sBuf.append("&mode=");
			    sBuf.append(modeLocation);
			    Log.i(MainActivity.TAG + getClass(), "URL: " + sBuf.toString());
			    parser = new GoogleParser(sBuf.toString());
			    Route r = parser.parse();
			    if (r != null) {
			      return r;
			    } else {
			      return null;
			    }
			  }

	public void set(ArrayList<String> listView2, String modeLocation2) {
		// TODO Auto-generated method stub
		listView=listView2;
		modelocation=modeLocation2;
		
	}
}
